﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using com.subb.WebInfo;
using WebEntities.Models.WebInfo;
using System.Configuration;
using System.IO;

namespace WebAdmin.Controllers
{
    [Authorize]
    public class FriendlyLinkController : Controller
    {
        //
        // GET: /FriendlyLink/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllFriendlyLink(StoreRequestParameters parms)
        {
            WI_FriendlyLinkDAL mdal = new WI_FriendlyLinkDAL();
            int ttlCount;
            List<WI_FriendlyLink> dbFriendlyLinklist = mdal.GetAllFriendlyLink(parms.Start, parms.Limit, out ttlCount);
            return this.Store(dbFriendlyLinklist, ttlCount);
        }


        [HttpPost]
        public ActionResult DeleteFriendlyLink(List<FriendlyLink> delFriendlyLink)
        {
            try
            {
                WI_FriendlyLinkDAL mdal = new WI_FriendlyLinkDAL();
                foreach (var FriendlyLink in delFriendlyLink)
                {
                    mdal.RemoveFriendlyLink(FriendlyLink.FLID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewFriendlyLinkWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建友情链接",
                Width = 400,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateFriendlyLink"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateFriendlyLink()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateFriendlyLink(FriendlyLink obj)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                //上传文件
                string savePath = string.Empty;
                string linkPath = string.Empty;
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("FriendlyLogo");

                    //上传产品封面图
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath += uploadVFolder + "FriendlyLink/";
                            linkPath += uploadVFolder + "FriendlyLink/";
                            savePath = Server.MapPath(savePath);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            string filename = DateTime.Now.Ticks + extName;
                            savePath += filename;
                            linkPath += filename;
                            fpfb1.SaveAs(savePath);

                        }
                    }
                }
                WI_FriendlyLinkDAL mdal = new WI_FriendlyLinkDAL();
                WI_FriendlyLink addObj = new WI_FriendlyLink()
                {
                    CreateTime = DateTime.Now,
                    FriendlyLogo = linkPath,
                    FriendlyName = obj.FriendlyName,
                    IsShow = byte.Parse(obj.IsShow.ToString()),
                    LinkClickCount = 0,
                    SortNum = obj.SortNum,
                    WebLink = obj.WebLink

                };
                mdal.CreateFriendlyLink(addObj);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "数据创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditFriendlyLinkWindow(int FriendlyLinkId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑友情链接",
                Width = 400,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutEdit,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditFriendlyLink"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("FriendlyLinkId", FriendlyLinkId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditFriendlyLink(int FriendlyLinkId)
        {
            WI_FriendlyLinkDAL mdal = new WI_FriendlyLinkDAL();
            WI_FriendlyLink dbFriendlyLink = mdal.GetFriendlyLink(FriendlyLinkId);
            FriendlyLink FriendlyLink = FriendlyLink.AsFriendlyLink(dbFriendlyLink);
            return View(FriendlyLink);
        }

        [HttpPost]
        public ActionResult EditFriendlyLink(FriendlyLink obj)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                //上传文件
                string savePath = string.Empty;
                string linkPath = string.Empty;
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("FriendlyLogo");

                    //上传产品封面图
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath += uploadVFolder + "FriendlyLink/";
                            linkPath += uploadVFolder + "FriendlyLink/";
                            savePath = Server.MapPath(savePath);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            string filename = DateTime.Now.Ticks + extName;
                            savePath += filename;
                            linkPath += filename;
                            fpfb1.SaveAs(savePath);

                        }
                    }
                }

                WI_FriendlyLinkDAL mdal = new WI_FriendlyLinkDAL();
                obj.FriendlyLogo = linkPath;
                mdal.UpdateFriendlyLink(obj.FLID, obj);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "数据更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

    }
}
